package me.fanwu;

import com.google.common.base.Objects;
import com.google.common.collect.Range;

import java.math.BigDecimal;

/*
 * Wupher 编写于 16/8/8.
 */
@SuppressWarnings(value = "all")
public class TimeSlice {
    private int startTime;
    private int endTime;
    private BigDecimal price;

    private Range<Integer> slice;

    public TimeSlice(int startTime, int endTime, BigDecimal price) {
        if (startTime < 0)
            throw new IllegalArgumentException("开始时间不能小于0, " + startTime);
        if (endTime < startTime) {
            //这里的TimSlice是仅用于消费时段切分的,此处的TimeSlice相当于计费时段设置,记录的是营业时间偏移量
            // ,开始一定是0,结束为1440
            throw new IllegalArgumentException("结束时间不能小于开始时间, 结束时间 " +
                    endTime + " 开始时间 " + startTime);
        }
        this.startTime = startTime;
        this.endTime = endTime;
        this.price = price;
        slice = Range.closedOpen(startTime, endTime);
    }

    public TimeSlice(int startTime, int endTime) {
        if (startTime < 0)
            throw new IllegalArgumentException("开始时间不能小于0, " + startTime);
        if (endTime < startTime) {
            //这里的TimSlice是仅用于消费时段切分的,此处的TimeSlice相当于计费时段设置,记录的是营业时间偏移量,开始一定是0,结束为1440
            throw new IllegalArgumentException("结束时间不能小于开始时间, 结束时间 " +
                    endTime + " 开始时间 " + startTime);
        }
        this.startTime = startTime;
        this.endTime = endTime;
        this.price = new BigDecimal(0);
        slice = Range.closedOpen(startTime, endTime);
    }

    public Range<Integer> getSlice() {
        return slice;
    }

    public int getStartTime() {
        return startTime;
    }

    public int getEndTime() {
        return endTime;
    }

    public BigDecimal getPrice() {
        return price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TimeSlice timeSlice = (TimeSlice) o;
        return startTime == timeSlice.startTime &&
                endTime == timeSlice.endTime;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(startTime, endTime);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("TimeSlice(");
        sb.append(startTime);
        sb.append(", ").append(endTime);
        sb.append(")");
        sb.append(" $").append(price);
        return sb.toString();
    }

    /**
     * 将时间变换成当前时间显示
     *
     * @param workTimeOffside 营业时间偏移量
     * @return 显示的字符串 (12:00 ~ 14:00)
     */
    public String displayInLocalTime(int workTimeOffside) {
        int currentStartTime = (startTime + workTimeOffside) % TimeCapculation.TIME_LIMIT;
        int currentEndTime = (endTime + workTimeOffside) % TimeCapculation.TIME_LIMIT;
        return "(" + modInStr(currentStartTime) + " - " + modInStr(currentEndTime) + " )" + " $ " + price;
    }


    private static String modInStr(int minuteOfDay) {
        int hours = minuteOfDay / 60;
        int minutes = minuteOfDay % 60;
        return String.format("%02d:%02d", hours, minutes);
    }


}
